#ifndef _SHT1X_H
#define _SHT1X_H

#include "stdbool.h"
#include "stdio.h"
#include "math.h"
#include "fw/include/mgos_gpio.h"
#include "fw/include/mgos_timers.h"
#include "fw/include/mgos_system.h"

#define port_data 13 // D7
#define port_sck 15  // D8

#define DATA_L mgos_gpio_write(port_data, false)
#define DATA_H mgos_gpio_write(port_data, true)
#define DATA_R mgos_gpio_read(port_data)
#define DATA_I                                           \
    mgos_gpio_set_mode(port_data, MGOS_GPIO_MODE_INPUT); \
    mgos_gpio_set_pull(port_data, MGOS_GPIO_PULL_NONE);
#define DATA_Q mgos_gpio_set_mode(port_data, MGOS_GPIO_MODE_OUTPUT)

#define SCK_L mgos_gpio_write(port_sck, false)
#define SCK_H mgos_gpio_write(port_sck, true)
#define SCK_Q mgos_gpio_set_mode(port_sck, MGOS_GPIO_MODE_OUTPUT)

#define noACK 0
#define ACK 1
//adr  command  r/w
#define STATUS_REG_W 0x06 //000   0011    0
#define STATUS_REG_R 0x07 //000   0011    1
#define MEASURE_TEMP 0x03 //000   0001    1
#define MEASURE_HUM 0x05  //000   0010    1
#define RESET 0x1e        //000   1111    0

typedef struct
{
    float t;
    float h;
    float t_true;
    float h_true;
    unsigned char mode;
    unsigned char error;
    unsigned char checksum;
} SHT1x_V3;

void connectionreset(void);
char softreset(void);
void measure(unsigned char mode);
void calc_sth11(SHT1x_V3 *sht1x);
SHT1x_V3 get_sht1x(void);

#endif // _SHT1X_H